#include "ServerWindow.h"

// Public Methods

ServerWindow::ServerWindow (QWidget * parent) : QMainWindow(parent)
{
	server = new PlaylistServer();

	setWindowTitle("SocialRadio - Server");
	setCentralWidget(new QWidget());
	resize(400, 400);

	initMenu();
	initMainLayout();
	updateGUI();
}

void ServerWindow::notify (Observable * obj, int eventCode)
{

}

// Slots

void ServerWindow::startServer ()
{
	server->runServer();
}

void ServerWindow::stopServer ()
{

}

void ServerWindow::addSong ()
{
	QString fileName = QFileDialog::getOpenFileName(this, tr("Choose audio file"),
					   "~", tr("Audio (*.wav *.mp3)"));

	Song song(fileName.toStdString());
	server->getPlaylist()->addSong(song);
}

// Private Methods

void ServerWindow::initMenu ()
{
	QMenu * serverMenu = menuBar()->addMenu("&Server");
	QAction * startAction = new QAction(tr("&Start"), this);
	QAction * stopAction = new QAction(tr("&Stop"), this);
	serverMenu->addAction(startAction);
	serverMenu->addAction(stopAction);

	QMenu * mediaMenu = menuBar()->addMenu("&Media");
	QAction * addSongAction = new QAction(tr("&Add Song..."), this);
	mediaMenu->addAction(addSongAction);

	QObject::connect(startAction, SIGNAL(triggered()), this, SLOT(start()));
	QObject::connect(stopAction, SIGNAL(triggered()), this, SLOT(stop()));
	QObject::connect(addSongAction, SIGNAL(triggered()), this, SLOT(addSong()));
}

void ServerWindow::updateMenu ()
{
}

void ServerWindow::initMainLayout ()
{
    mainLayout = new QVBoxLayout();

	QRadioButton * playlistRadio = new QRadioButton(tr("Stream playlist"));
	mainLayout->addWidget(playlistRadio);

	playlistWidget = new PlaylistWidget(server->getPlaylist());
	playlistWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
    mainLayout->addWidget(playlistWidget);

	QRadioButton * micRadio = new QRadioButton(tr("Stream microphone"));
	mainLayout->addWidget(micRadio);

	mainLayout->addStretch();
}

void ServerWindow::updateGUI ()
{
	centralWidget()->setLayout(mainLayout);
}
